/*
 * keyboard.cpp
 *
 *  Created on: 2021年3月12日
 *      Author: root
 */

#include <drivers/keyboard.h>
using namespace myos::drivers;
using namespace myos::common;
using namespace myos::hardwarecommunication;



KeyboardEventHandle::KeyboardEventHandle(){

}
KeyboardEventHandle::~KeyboardEventHandle(){

}

void KeyboardEventHandle::OnKeyDown(char ){

}
void KeyboardEventHandle::OnKeyUP(char ){

}

void printf(char* str);
void printfHex(uint8_t key);

KeyboardDrive::KeyboardDrive(InterruptManager* manager ,KeyboardEventHandle* handler)
:InterruptHandler(0x21,manager),
 dataport(0x60),
 commandport(0x64)
{

	this->handler=handler;
}

KeyboardDrive::~KeyboardDrive(){

}

void KeyboardDrive::Activate(){
	//CPU接受来着键盘的信息,如果没有就会一直等待
		while(commandport.Read() & 0x1){
			dataport.Read();
		}
		commandport.Write(0xAE); //active interrupts 键盘发出中断
		/*
		 *	中断执行完了，马上就保存现场，然后返回中断端口
		 *	这个给一个当前的状态,就是用来判断是否已经保存了中断现场？
		 */
		commandport.Write(0x20); //get current state
		uint8_t status=(dataport.Read() | 1) & ~0x10;  //记录当前状态
		commandport.Write(0x60); //set state   发出指令让CPU进入新的状态
		dataport.Write(status);		//同时数据窗口接受数据

		dataport.Write(0xF4); 	//整个中断流程走完了，要反回之前的中断现场
}
uint32_t KeyboardDrive::HandleInterrupt(uint32_t esp){
	uint8_t key=dataport.Read();
	static bool Shift=false;
	if(handler==0){
		return esp;
	}
switch(key){

	case 0x02: handler->OnKeyDown('1'); break;
	case 0x03: handler->OnKeyDown('2'); break;
	case 0x04: handler->OnKeyDown('3'); break;
	case 0x05: handler->OnKeyDown('4'); break;
	case 0x06:handler->OnKeyDown('5'); break;
	case 0x07:handler->OnKeyDown('6'); break;
	case 0x08:handler->OnKeyDown('7'); break;
	case 0x09:handler->OnKeyDown('8'); break;
	case 0x0A:handler->OnKeyDown('9'); break;
	case 0x0B:handler->OnKeyDown('0'); break;

	case 0x10:handler->OnKeyDown('q'); break;
	case 0x11:handler->OnKeyDown('w'); break;
	case 0x12:handler->OnKeyDown('e'); break;
	case 0x13:handler->OnKeyDown('r'); break;
	case 0x14:handler->OnKeyDown('t'); break;
	case 0x15:handler->OnKeyDown('y'); break;
	case 0x16:handler->OnKeyDown('u'); break;
	case 0x17:handler->OnKeyDown('i'); break;
	case 0x18:handler->OnKeyDown('o'); break;
	case 0x19:handler->OnKeyDown('p'); break;

	case 0x1E:handler->OnKeyDown('a'); break;
	case 0x1F:handler->OnKeyDown('s'); break;
	case 0x20:handler->OnKeyDown('d'); break;
	case 0x21:handler->OnKeyDown('f'); break;
	case 0x22:handler->OnKeyDown('g'); break;
	case 0x23:handler->OnKeyDown('h'); break;
	case 0x24:handler->OnKeyDown('j'); break;
	case 0x25:handler->OnKeyDown('k'); break;
	case 0x26:handler->OnKeyDown('l'); break;

	case 0x2C:handler->OnKeyDown('z'); break;
	case 0x2D:handler->OnKeyDown('x'); break;
	case 0x2E:handler->OnKeyDown('c'); break;
	case 0x2F:handler->OnKeyDown('v'); break;
	case 0x30:handler->OnKeyDown('b'); break;
	case 0x31:handler->OnKeyDown('n'); break;
	case 0x32:handler->OnKeyDown('m'); break;

	case 0x33:handler->OnKeyDown(','); break;
	case 0x34:handler->OnKeyDown('.'); break;
	case 0x35:handler->OnKeyDown(':'); break;

	case 0x1C: handler->OnKeyDown('\n');break;
	case 0x39: handler->OnKeyDown(' ');break;
	case 0x2A: case 0x36 : Shift=true; break;
	case 0xAA: case 0xB6: Shift=false;break;

	case 0x45:
		break;

	default:
		printf("KEYBOARD 0x");
		printfHex(key);
		break;
}
	return esp;
	}



